﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Tool.Log;

namespace zdmacs
{
    public class HttpUtils
    {
        public string HttpGet(string url) {
            string ret = null;
            try
            {
                System.Net.HttpWebRequest request;
                // 创建一个HTTP请求
                request = (System.Net.HttpWebRequest)WebRequest.Create(url);
                //request.Method="get";
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string responseText = myreader.ReadToEnd();
                myreader.Close();
                ret = responseText;
            }
            catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            
            return ret;
        }
 }

    public class PostItem
    {
        public String Key;
        public String Value;
    }

    public class HttpPostBean
    {
        private const string BOUNDARY = "--HEDAODE--";

        private List<PostItem> _posts;
        private List<PostItem> _files;
        public String Url = "";
        public HttpPostBean(string Url)
        {
            _posts = new List<PostItem>();
            _files = new List<PostItem>();
            this.Url = Url;
        }

        public static string UrlEncode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }

            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)


            return HttpUtility.UrlEncode(byStr, 0, byStr.Length);
        }

        public PostItem FindPostItem(String item)
        {
            for (int i = 0; i < _posts.Count; i++)
            {
                if (String.Compare(_posts[i].Key, item, true) == 0)
                {
                    return _posts[i];
                }
            }
            return null;
        }

        public bool IsKeyExist(String key)
        {
            for (int i = 0; i < _posts.Count; i++)
            {
                if (String.Compare(_posts[i].Key, key, true) == 0)
                {
                    return true;
                }
            }
            return false;
        }

        public String GetKeyValue(String key)
        {
            for (int i = 0; i < _posts.Count; i++)
            {
                if (String.Compare(_posts[i].Key, key, true) == 0)
                {
                    return _posts[i].Value;
                }
            }
            return "";
        }

        public void AddKeyValue(String key, String value)
        {
            PostItem item = FindPostItem(key);
            if (item != null)
            {
                item.Value = value;
            }
            else
            {
                item = new PostItem() { Key = key, Value = value };
                _posts.Add(item);
            }
        }

        public PostItem FindFilePostItem(String item)
        {
            for (int i = 0; i < _files.Count; i++)
            {
                if (String.Compare(_files[i].Key, item, true) == 0)
                {
                    return _files[i];
                }
            }
            return null;
        }


        public String Request()
        {

            LogHelper.WriteLog(Url);
            String paramData = GetPostParams();
            LogHelper.WriteLog(paramData);
            byte[] byteArray = Encoding.UTF8.GetBytes(paramData);

            try
            {
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(Url));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();

                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                String responseText = sr.ReadToEnd();
                sr.Close();
                response.Close();

                LogHelper.WriteLog(responseText);
                return responseText;
            }
            catch (Exception ex)
            {
            }
            return "";
        }

        public String GetPostParams()
        {
            String param = "";
            for (int i = 0; i < _posts.Count; i++)
            {
                if (string.IsNullOrEmpty(param) == false)
                {
                    param += "&";
                }

                param += _posts[i].Key;
                param += "=";
                param += _posts[i].Value;
            }
            return param;
        }
    }
}
